<?php

namespace backend\controllers;

use backend\models\ErrorImg;
use backend\models\PicTemp;
use backend\models\Portray;
use common\models\Picture;
use Yii;
use backend\models\StarTemp;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * StarTempController implements the CRUD actions for StarTemp model.
 */
class StarTempController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }



    /*
     * 抓取失败的图片日志中提取出url 放入LIST
     */
    public function actionDealErrorLog()
    {
        exit;
        $i = (int)Yii::$app->request->get('page');
        $i < 1 && $i = 1;
        $file = fopen("/mnt/hgfs/VMShare/crawl_{$i}.log","r");


        while (!feof($file)) {
            $error = fgets($file);
            $error = strstr($error,'https:');
            $error = mb_substr($error,0,strpos($error,'.jpg') + 4);
            $key = 'errorImgs';
            if(!empty($error)){
                  Yii::$app->redis->LPush($key,$error);
//                $model = new ErrorImg();
//                $model->setAttribute('img',$error);
//                $model->save();
            }
        }
        fclose($file);
    }


    public function actionRangeIds(){
        $count = 0;
        for($i = 0; $i < 480; $i++){
            $img = Yii::$app->redis->LPop('errorImgList');
            $imgs = explode('/',$img);
            $old_id = intval($imgs[4]);
            $new_id = (int)Yii::$app->redis->HGet("IdRelation",$old_id);

            if($new_id > 0) {
                Yii::$app->redis->LPush('errorImgs',$img);
                $count++;
            }
        }
        // 500/110S
        $second = $count / 500 * 110 * 1000;
        return $this->render('fuckimg',['count'=>$count,'second'=>$second]);
    }


    public function actionDealErrorImg(){
        $count = 0;
        for($i = 0 ; $i < 350; $i++){
            $img = Yii::$app->redis->RPop('goErrorImg');
            $imgInfo = explode('/',$img);
            $old_id = intval($imgInfo[4]);
            $new_id = (int)Yii::$app->redis->HGet('IdRelationNew',$old_id);
            if($new_id > 0){
                Yii::$app->redis->RPush('tmpImgs',$img);
                //Yii::$app->redis->LPush('RightImgs',$img);
                $count++;
            }else{
                Yii::$app->redis->LPush('NoNewId',$img);
            }
        }
        $time = $count / 500 * 110 * 1000;
        echo $count,',',$time;
        return $this->render('fuckimg',['time'=>$time * 1.2]);

    }


    public function actionFuckImga(){
        $query = StarTemp::find();
        $max = (int)Yii::$app->redis->Get('maxPicTempID');
        $query->andWhere(['>','new_id',0]);
        $query->andWhere(['>','id',$max]);
        $data = $query->select('new_id,old_id,id')->asArray()->all();

        foreach ($data as $vo){
            Yii::$app->redis->HSet("IdRelationNew",$vo['old_id'],$vo['new_id']);
            YII::$app->redis->Set('maxPicTempID',$vo['id']);
        }
    }

    public function actionFuckImg(){


        //http://www.guotunet.com/star-temp/fuck-img
        $query = PicTemp::find();

        $max = (int)Yii::$app->redis->Get('maxPicTempID');


        $query->andWhere("content not like '%t1.onvsh%'")->andWhere(['>','new_id',0]);
        $query->andWhere(['>','id',$max]);

        //处理新旧ID关系 放入Redis
        $query->limit(10000);
        $data = $query->select('new_id,old_id,id')->asArray()->all();
        foreach ($data as $vo){
            Yii::$app->redis->HSet("IdRelation",$vo['old_id'],$vo['new_id']);
            YII::$app->redis->Set('maxPicTempID',$vo['id']);
        }

        exit;


        $query->limit(110);
        $query->orderBy('id asc');
        $query->select('id');
        $data = $query->asArray()->all();

        foreach ($data as $vo){

            $id = intval($vo['id']);
            //$this->RedisHMset('PicTemp:'.$id,$vo);
            YII::$app->redis->LPush('PicTempIdList',$id);
            YII::$app->redis->Set('maxPicTempID',$id);
            echo $id,',';
        }

        return $this->render('fuckimg');
    }

    /*
     * 临时相册数据放入redis 配合GO采集图片
     */
    public function actionPicTempIntoRedis(){
        $query = PicTemp::find();

        $max = (int)Yii::$app->redis->Get('maxPicTempID');


        $query->andWhere("content not like '%t1.onvsh%'")->andWhere(['>','new_id',0]);
        $query->andWhere(['>','id',$max]);
        $query->limit(110);
        $query->orderBy('id asc');
        $query->select('id');
        $data = $query->asArray()->all();

        foreach ($data as $vo){

            $id = intval($vo['id']);
            //$this->RedisHMset('PicTemp:'.$id,$vo);
            YII::$app->redis->LPush('PicTempIdList',$id);
            YII::$app->redis->Set('maxPicTempID',$id);
            echo $id,',';
        }

    }

    function RedisHMset($redisKey = '',$data = []){
        if(empty($data)) return;
        foreach ($data as $key => $value){
            Yii::$app->redis->HSet($redisKey,$key,$value);
        }
    }

    /*
     * 待处理女星数据正式入库 ok
     */
    public function actionDeal(){
        $query = StarTemp::find();
        $query->andFilterWhere(['new_id'=>0]);
        $tempDatas = $query->limit(1000)->asArray()->all();



        foreach ($tempDatas as $tmp){
            $model = StarTemp::findOne($tmp['id']);
            $rs = $model->dealWithTempStarInfo($tmp);
            echo $rs;
        }
    }

    /*
     * 女星头像下载 ok
     */
    public function actionDealImg(){
        $query = StarTemp::find();

        $query->andWhere(['local_header'=>null]);
        $query->select(['id','header','url','new_id']);
        $_id = (int)Yii::$app->request->get('id');

        if($_id > 0){
             $query->limit($_id,$_id + 500);
        }else{
            $query->limit(500);
        }





        $data = $query->asArray()->all();




        $len = count($data);
        for($i = 0; $i < $len; $i++){
            if(!file_exists(Yii::getAlias('@storage').'/star/'.$data[$i]['new_id']))
            mkdir(Yii::getAlias('@storage').'/star/'.$data[$i]['new_id'],'777');
            $fs = fopen(Yii::getAlias('@storage').'/star/'.$data[$i]['new_id'].'/'.'header.jpg',"w+");
            fwrite($fs,$this->getImage($data[$i]['header'],$data[$i]['url']));
            fclose($fs);
            $model = $this->findModel($data[$i]['id']);
            $model->setAttributes(['local_header'=>'/star/'.$data[$i]['new_id'].'/'.'header.jpg']);
            $model->save();
            echo $data[$i]['id'],'<br/>';
        }
    }


    /*
     * 抓取遠程圖片 效率不行
     */
    function getImage($uri,$from){
        ob_start();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $uri);
        if($from){
            curl_setopt($ch,CURLOPT_REFERER, $from);
        }
        curl_exec($ch);
        $img = ob_get_contents();
        curl_close($ch);
        ob_clean();
        return $img;
    }


    /*
     * 解析抓取URL中的ID
     */
    public function actionParseId(){
        $query = StarTemp::find();
        $query->andWhere(['old_id'=>0]);
        $query->limit(1000);
        $query->select(['id','url']);
        $data = $query->asArray()->all();

        foreach ($data as $vo){
            $url = explode('/',$vo['url']);
            $old_id = intval($url[4]);
            if($old_id > 0){
                $model = $this->findModel($vo['id']);
                $model->setAttribute('old_id',$old_id);
                $model->save();
                echo $old_id.',';
            }
        }

    }

    /*
     * 解析tag中的name出来
     */
    public function actionParseName(){
        $query = PicTemp::find();
        $query->andWhere(['name'=>null])->andWhere(['not like','tags',',%'])->limit(10000);
        $data = $query->select(['id','tags','url'])->asArray()->all();



        foreach ($data as $vo){
            $tags = explode(',',trim($vo['tags']));
            if(is_array($tags)  && count($tags > 0)){
                $model = PicTemp::findOne(intval($vo['id']));
                $model->setAttribute('name',trim($tags[0]));
                $model->save();
                echo $tags[0].'<br/>';
            }
        }

        exit;

    }


    /*
     * 根据ID住区漏下的女星信息
     */
    public function actionNewStar(){
        exit;
        $star = 15642;
        $end = 24730;

        $query = StarTemp::find();
        $data = $query->select('old_id')->asArray()->all();
        $rs = [];
        foreach ($data as $vo){
            $rs[] = $vo['old_id'];
        }

        $checkIds = [];//遗漏的ID
        for($i = $star;$i<=$end;$i++){
            if(!in_array($i,$rs)){
                $checkIds[] = $i;
            }
        }


        $needSpiderLink = [];
        foreach ($checkIds as $id){
            $url = "https://www.nvshens.com/girl/{$id}/";
//            if($this->check404($url));
            $needSpiderLink[] = $url;
        }
        print_r($needSpiderLink);exit;
        exit;
        file_put_contents("/mnt/hgfs/workSpace/github/guotunet/storage/needSpiderLink.txt",json_encode($needSpiderLink));exit;


        file_put_contents('/mnt/hgfs/workSpace/github/guotunet/storage/links.txt',json_encode($needSpiderLink));


    }

    function check404($url){
        header("Content-type:text/html;charset=utf-8");
        $content = file_get_contents($url);

        if(strpos($content,'您查找的页面不存在')==true){
            unset($content);
            return false;
        }
        return true;
    }




    /**
     * Lists all StarTemp models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => StarTemp::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single StarTemp model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);

        return $this->render('view', [
            'model' => $model,
        ]);
    }

    /**
     * Creates a new StarTemp model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new StarTemp();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing StarTemp model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);


        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing StarTemp model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the StarTemp model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return StarTemp the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = StarTemp::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }


    /**
     * 抓取的图片地址处理成本地图片地址
     */
    public function actionFinallyDealTempImg(){
        $query = PicTemp::find();
        $query->where("local_content not like '%jpg%' ")->limit(5000)->select(['id','page','content','local_content'])->orderBy('old_id,page asc');
        $data = $query->asArray()->all();



        $i = 0;
        foreach ($data as $vo){
            $temp = explode(',',$vo['content']);
            if(is_array($temp) && !empty($temp)){
                foreach ($temp as $tk=>$tv){
                    $temp[$tk] = explode('/',$tv)[7];
                }
            }
            $model = PicTemp::findOne($vo['id']);
            $model->setAttribute('local_content',json_encode($temp));
            $model->save();
            unset($temp);
            $i++;
        }
        echo $i;
    }

    /**
     * 写真数据放入Redis 先入redis后入数据库
     */
    public function actionSetPic(){
        $data = PicTemp::find()->limit(5000)->where('in_redis = 0 and status = 1 and new_id > 0')->orderBy('old_id,page asc')->asArray()->all();
        $redis = Yii::$app->redis;
        $newId = (int)$redis->Get('newId');

        foreach ($data as $vo){
            $lastInfo = $redis->HMget('lastInfo','newId','oldId');
            if($vo['old_id'] == $lastInfo[1] && $vo['new_id'] == $lastInfo[0]){
                $redisKey = 'portray:'.$newId;
                $images = json_decode($redis->HGet($redisKey,'images'),true);
                $_images = json_decode($vo['local_content'],true);

                foreach ($_images as &$_vo){
                    $_vo = $vo['old_id'].'/'.$_vo;
                }unset($_vo);

                $images = array_merge($images,$_images);
                $image_num = count($images);
                $redis->HMset($redisKey,'images',json_encode($images),'image_num',$image_num);
            }else{
                $newId = $redis->Incr('newId');
                $redisKey = 'portray:'.$newId;
                $view = intval($vo['view']) + rand(300,1000);
                $images = json_decode($vo['local_content'],true);
                foreach ($images as &$_vo){
                    $_vo = $vo['old_id'].'/'.$_vo;
                }unset($_vo);

                $image_num = count($images);
                $tags = trim($vo['tags']);
                if(stripos($tags,",") == 0){
                    $tags = mb_substr($tags,1);
                }

                $redis->HMset($redisKey,
                                'id',$newId,
                                'title',trim($vo['title']),
                                'desc',trim($vo['desc']),
                                'tags',$tags,
                                'view',$view,
                                'create_time',intval($vo['create_time']),
                                'status',intval($vo['status']),
                                'star_id',intval($vo['new_id']),
                                'image_num',$image_num,
                                'images',json_encode($images)
                              );
                $redis->HMset('lastInfo','newId',$vo['new_id'],'oldId',$vo['old_id']);
            }

            $model = PicTemp::findOne($vo['id']);
            $model->setAttribute('in_redis',1);
            $model->save();

//            $redisKey = 'portray:'.$newId;
//            if($redis->Exists($redisKey) ){
//                $images = json_decode($redis->hGet($redisKey,'images'),true);
//
//            }
            //$redis->HMset('lastInfo','newId',22,'oldId',33);
        }
    }


    /**
     * 写真数据入库
     */
    public function actionPicInMysql(){
        $startId = Yii::$app->redis->Get('insertId');
        for($i = $startId + 1;$i < $startId + 5000;$i++){
            $data = Yii::$app->redis->HGetall('portray:'.$i);
            $data = $this->parseRedisData($data);
            if($data){
                $model = new Portray();
                $model->setAttributes($data);
                $model->save();
                Yii::$app->redis->Set('insertId',$i);
            }

        }
    }

    public function parseRedisData($data = []){
        $rs = [];
        if(!is_array($data) || empty($data)) return $rs;

        $count = count($data);
        for ($i = 0 ; $i < $count ; $i++){
            if($i % 2 != 0){
                $rs[$data[$i-1]] = $data[$i];
            }
        }
        return $rs;
    }





}
